﻿//<reference path="/Scripts/References.js" />
//<reference path="/ScriptsG/en-us/InnovacallDynamic.js" />

//#region local variables
tinyMCEElements = "divFooEditor";
//#endregion

//#region default values
var ribbonTabCount = 1;
//#endregion

//#region DocReady
$.fn.DocReady = function () {
    /// <summary>
    /// Scripts to execute when all page is loaded
    /// </summary>
    language = $("#hidLanguage").val();
    tpLang = uiLanguage.substr(0, 2);
    if (tpLang != 'en') {
        //$.timepicker.setDefaults($.timepicker.regional[tpLang]);
    }
    $("body").addClass("businessFeatureMode");
    $.fn.PrepareLayoutMenus(); //_layout.js
    $("#divMceRibbonBarPlaceholder").removeClass("notDisplayed");
    $.fn.TinyMCECreateTinyMCEInstance();
    UseMode.setCurrent(enUseModes.testMode);
}
//#endregion

//#region UseMode

var enUseModes = {
    "testMode": "testMode",
    "manageMode": "manageMode"
};

var UseMode = {
    current: enUseModes.contentEditMode,
    getCurrent: function () {
        return this.current;
    },
    setCurrent: function (value) {
        $("body").removeClass(this.current);
        this.current = value;
        $("body").addClass(this.current);
        console.log("UseMode:" + this.current);
        $("#te-panel").css("display", "none");
        switch (this.current) {
            case enUseModes.testMode:
                $.fn.TinyMCEShow(false);
                break;
            case enUseModes.manageMode:
                $.fn.TinyMCEShow(false);
                break;
        }
    }
};


//#endregion

//#region TinyMCE stuff

$.fn.TinyMCECreateTinyMCEInstance = function () {
    /// <summary>
    /// Creates the Tiny MCE instance
    /// </summary>
    var editorLang = uiLanguage.substring(0, 2);
    if ($("#divFooEditor").length == 0) {
        var htmlBribe = "<div id='divFooEditor' class='notDisplayed' >&nbsp;</div>";
        $("body").append(htmlBribe);
    }
    if (tinyMCEElements == undefined) {
        editorWidth = 0;
        editorHeight = 0;
    } else {
        if (tinyMCEElements == "divFooEditor") {
            editorWidth = 0;
            editorHeight = 0;
        } else {
            var element = $("#" + tinyMCEElements);
            editorWidth = element.width();
            editorHeight = element.height();
        }
    }
    tinyMCE.init
    (
        {
            // General options
            elements: tinyMCEElements,
            mode: "exact",
            language: editorLang,
            theme: "ribbon",
            plugins: "inlinepopups,tabfocus,advimagescale,loremipsum,image_tools,embed,tableextras,style,table,searchreplace,contextmenu,paste,wordcount,advlist,autosave,save,layer,table",//,noneditable,visualblocks",
            //inlinepopups_skin: 'ribbon_popup',
            theme_ribbon_fonts: "Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats,Boing=kids_boing",
            noneditable_leave_contenteditable: false,
            width: editorWidth,
            height: editorHeight,
            // Theme options WARNING No space in tab caption
            theme_ribbon_tab1: {
                title: cap_Ribbon_Tab01_Test,
                items:
                [
                    ["innoToggleEdit_Tab00"],
                    ["innoSave_Tab00"],
                ]
            },
            //Absolute path WARNING
            //With URL Rewriting path mus be relative to root but not include authority.
            //The right setting for convert_url = false.
            remove_script_host: false,
            convert_urls: false,
            // Skin options
            //////////skin: "o2k7",
            //////////skin_variant: "silver",
            // Example content CSS (should be your site CSS)
            content_css: "/Content/EditableContent/Base.css?" + new Date().getTime(),
            //Drop lists for link/image/media/template dialogs
            //template_external_list_url: "js/template_list.js",
            //external_link_list_url: "js/link_list.js",
            //external_image_list_url: "js/image_list.js",
            //media_external_list_url: "js/media_list.js",
            file_browser_callback: "$.fn.TinyMCEFilebrowser",
            setup: $.fn.TinyMCERibbonCustomSetup
        }
    );
    //Detect Ribbon tab click
    $(".mce_innoSave").addClass("big");
}

$.fn.TinyMCEFilebrowser = function (field_name, url, type, win) {
    /// <summary>
    /// Tiny MCE File browser handler
    /// </summary>
    /// <param name="field_name"></param>
    /// <param name="url"></param>
    /// <param name="type"></param>
    /// <param name="win"></param>
    /// <returns type=""></returns>
    var fileBrowserURL = "/" + uiLanguage + "/finder";
    var sessionId = $("#hidSessionId").val();

    tinyMCE.activeEditor.windowManager.open({
        title: "Ajax File Manager",
        url: fileBrowserURL,
        width: 950,
        height: 650,
        inline: "yes",
        maximizable: "no",
        popup_css: false,
        close_previous: "yes"
    }, {
        window: win,
        input: field_name,
        sessionid: sessionId,
        resizable: "no"
    });
    return false;
}

$.fn.TinyMCEHideAllInstance = function () {
    /// <summary>
    /// Hide all tiny mce instances
    /// </summary>
    for (var i = 0; i < tinymce.editors.length; i++) {
        tinyMCE.get(tinymce.editors[i].id).hide();
    };
}

$.fn.TinyMCEInitTinyMCE = function (ed) {
    /// <summary>
    /// TinyMCE initialization
    /// </summary>
    /// <param name="ed">TinyMCE instance</param>
    var $ed = $("#" + ed.id + "_tbl");
    var $tbr = $ed.find("tr:nth(1)");
    var keepWidth = parseInt($ed.parent().parent().width());
    /*globals*/
    var $ifr = $("#" + ed.id + "_ifr");
    /**/

    $ifr.attr("marginwidth", "0");
    $ifr.attr("marginheight", "0");
    $ifr.css("margin", "0");
    $ifr.css("padding", "0");
    $ifr.css("background", "transparent");

    var divMceRibbonBarPlaceholderInitialHtml = '';
    divMceRibbonBarPlaceholderInitialHtml += '<span class="mceEditor defaultSkin">\n';
    divMceRibbonBarPlaceholderInitialHtml += '\t<table class="mceLayout">\n';
    divMceRibbonBarPlaceholderInitialHtml += '\t</table>\n';
    divMceRibbonBarPlaceholderInitialHtml += '</span>\n';
    $("#divMceRibbonBarPlaceholder").html(divMceRibbonBarPlaceholderInitialHtml);
    $rbnPH = $("#divMceRibbonBarPlaceholder").find("table");
    $ribbonTable = $("#divMceRibbonBarPlaceholder").find(":nth-child(1)").find(":nth-child(1)");
    $ribbonTable.css("width", "100%");
    $ribbonTable.css("margin-left", "0px");
    $ribbonTable.css("margin-right", "0px");
    $tbr.hide();
    $ed.css("width", keepWidth + "px");
    $ifr.css("width", keepWidth + "px");
    $rbnPH.append($tbr);
    $tbr.show();
    $(".AdminModeMenuButtonRight,.AdminModeMenuButtonLeft").css("display", "none");
    $.fn.TinyMCEInstanceInitialized();
}

$.fn.TinyMCEAddLayoutStyle = function () {
    /// <summary>
    /// Add layout green borders
    /// </summary>
    /// <param name="ed"></param>
    var ifr = $("#divFooEditor_ifr");

    var d = ifr[0].contentWindow.document;
    //d.open(); d.close();
    // now start doing normal jQuery:
    var editorSpecificStyles = "<style type='text/css' id='CSSExtension_ifr'>\n";
    editorSpecificStyles += "html, body {margin:0;padding:0;}\n";
    //editorSpecificStyles += ".ifrEditorFix {padding:5px;}\n";
    editorSpecificStyles += "</style>\n";
    if ($("head", d).find("#CSSExtension_ifr").length > 0) {
        $("head", d).find("#CSSExtension_ifr").remove();
    }
    $("head", d).append(editorSpecificStyles);
    $("body", d).removeClass("mceContentBody");
    //$("body", d).addClass("ifrEditorFix");

    //var ifrHtml = ifr.find("html");
    //var ifrHead = ifrHtml.find("head");
    //var ifrBody = ifr.find("#tinymce");

    //ifrHtml.css("height", "90%");
    //ifrBody.removeClass("mceContentBody");
    //ifrBody.addClass("ifrEditorFix");
}

$.fn.TinyMCEResizeActiveEditor = function (parent) {
    /// <summary>
    /// Resize TinyMCE Active editor width and/of height
    /// </summary>
    /// <param name="parent">resize editor to the parent size according to paddings</param>

    //Obsolete: divFooEditor is hidden during Site layouting

    //EditorWidth = parent.width();// - (layoutVar_divBodyPaddingLeft + layoutVar_divBodyPaddingRight);
    //EditorHeight = parent.height();// - layoutVar_divBodyPaddingTop - layoutVar_divBodyPaddingBottom - 50;
    //try {
    //    tinymce.EditorManager.activeEditor.theme.resizeTo(EditorWidth, EditorHeight);
    //} catch (e) { }
}

$.fn.TinyMCERibbonCustomSetup = function (ed) {
    /// <summary>
    /// Customization of TinyMCE Ribbon
    /// </summary>
    /// <param name="ed">TinyMCE Instance</param>

    ed.onKeyDown.add(function (ed, e) {
        var editedLayoutSlot = $("#" + lastLayoutSlotId);
        var nextLayoutSlotToEdit;

        switch (e.keyCode) {
            case 9: //Tab
                if (("#divContent .layoutSlot").length > 0) {
                    if (e.shiftKey) {
                        //Previous layoutSlot
                        nextLayoutSlotToEdit = editedLayoutSlot.closest("li").prev();
                        if (nextLayoutSlotToEdit.length == 0) {
                            nextLayoutSlotToEdit = editedLayoutSlot.closest("ul").children().last();
                        }
                    } else {
                        //Next layoutSlot
                        nextLayoutSlotToEdit = editedLayoutSlot.closest("li").next();
                        if (nextLayoutSlotToEdit.length == 0) {
                            nextLayoutSlotToEdit = editedLayoutSlot.closest("ul").children().first();
                        }
                    }
                }
                var nextLayoutSlotToEditId = nextLayoutSlotToEdit.find(".mceEditable").attr("id");
                $.fn.TinyMCEShow(true, nextLayoutSlotToEditId);
                //Scrolling
                $("#" + nextLayoutSlotToEditId).scrollintoview(true);
                e.preventDefault();
                return false;
                break;
        }
    });

    ed.onInit.add($.fn.TinyMCEInitTinyMCE);
    ed.onInit.add($.fn.TinyMCEAddLayoutStyle);
    // Add a custom button
    for (var i = 0; i < ribbonTabCount; i++) {
        ed.addButton
        (
            "innoToggleEdit_Tab0" + i,
            {
                "label": cap_Ribbon_TogglePreview,
                "title": cap_Ribbon_TogglePreview_Title,
                "class": "big innoToggleEdit_Edit",
                "onclick": $.fn.RibbonEditionTypeWritingToggleEditModeClick
            }
        );
        ed.addButton
        (
            "innoSave_Tab0" + i,
            {
                "label": cap_Ribbon_Save,
                "title": cap_Ribbon_Save_Title,
                "class": "big innoSave",
                "onclick": $.fn.RibbonEditionTypeWritingSaveContentClick,
                "value": i
            }
        );
    }
}

$.fn.TinyMCEInstanceInitialized = function () {
    /// <summary>
    /// Occurs when tiny mce is initialized
    /// Ribbon Sliders
    /// </summary>

    //$(".ribbon_tab_btn li").on("click", $.fn.RibbonTabClick);
    //var layoutToolBar = $("#divMceRibbonBarPlaceholder").find("div[rel=Layout]");
    //var spacingSlidersDiv = $(layoutToolBar.find("div.ribbon_group")[4]);
    //spacingSlidersDiv.addClass("spacingRibbonPanel");
    //spacingSlidersDiv.html("");
    //spacingSlidersDiv.append("<ul><li><label>Margin</label><span id='SliderMargin'></span></li><li><label>Padding</label><span id='SliderPadding'></span></li></ul>");
    //spacingSlidersDiv.find("li").each(function () { $(this).find("span").slider({ slide: $.fn.TinyMCERefreshContentSpacing }); });

    $("#divFooEditor_resize").hide();
    $("#divFooEditor_parent").css("display", "block");

    //fix the iframe mode
    var ifr = $("#" + tinyMCEElements + "_ifr");
    //ifr.css("margin-left", "-1px");
    //ifr.css("margin-right", "-1px");
    //ifr.css("padding-top", "14px");

    //$("body", d).css("padding-top", layoutVar_divBodyPaddingTop + "px");
    //$("body", d).css("padding-left", layoutVar_divBodyPaddingLeft + "px");
    //$("body", d).css("padding-right", layoutVar_divBodyPaddingRight + "px");
    //$("body", d).css("padding-bottom", layoutVar_divBodyPaddingBottom + "px");
    //$.fn.TinyMCEResizeActiveEditor($("#MyPrettyCMSContent"));
    var d = ifr[0].contentWindow.document;
    var htmlBribe = '';
    //Content layout transfert

    //htmlBribe = '<link href="/Content/ClientComponents/gridster/jquery.gridster.css" rel="stylesheet" type="text/css" />';
    //$("head", d).append(htmlBribe);
    //htmlBribe = '<link href="/Content/ClientComponents/gridster/jquery.gridster.innovacall.css" rel="stylesheet" type="text/css" />';
    //$("head", d).append(htmlBribe);
    //$.fn.EditionTypeWritingUpdateEditorCSSExtension($("#hidCSSExtension").val());

    //$.fn.ContentLayoutPrepareCSSExtension();
    //$.fn.EditionTypeWritingUpdateContentCSSExtension($("#hidCSSExtension").val());
    $("#divFooEditor_tbl").addClass("notDisplayed");
}

$.fn.TinyMCERefreshContentSpacing = function () {
    /// <summary>
    /// Occurs when user change content lines spacing
    /// ToDo LJO : improve it to store settings in CSS file
    /// REMOVED
    /// </summary>
    var ifr = $("#divFooEditor_ifr");
    var d = ifr[0].contentWindow.document;
    switch ($(this).attr("id")) {
        case "SliderMargin":
            $(".mceEditable", d).css("margin", $(this).slider("value") + "px");
            break;
        case "SliderPadding":
            $(".mceEditable", d).css("padding", $(this).slider("value") + "px");
            break;
    }
}

$.fn.TinyMCEGetContentHack = function () {
    var toReturn = "";
    var ifr = $("#divFooEditor_ifr");
    var d = ifr[0].contentWindow.document;
    toReturn = $("body", d).html();
    return toReturn;
}

$.fn.TinyMCESetContentHack = function (content) {
    var toReturn = "";
    var ifr = $("#divFooEditor_ifr");
    var d = ifr[0].contentWindow.document;
    toReturn = $("body", d).html(content);
    return toReturn;
}

$.fn.TinyMCEMoveInstanceToDiv = function (evt, ui) {
    /// <summary>
    /// Recreate TinyMCE instance on the good div
    /// </summary>
    /// <param name="evt"></param>
    /// <param name="ui"></param>
    switch (UseMode.getCurrent()) {
        case enUseModes.structureEditMode:
        case enUseModes.contentCodeMode:
        case enUseModes.contentLayoutMode:
        case enUseModes.settingsMode:
        case enUseModes.siteLayoutMode:
            //Content edit not allowed
            break;
        default:
            var clickTargetId = $(this).attr("id")
            $.fn.TinyMCEShow(true, clickTargetId);
            break;
    }
}

$.fn.TinyMCEShow = function (showIt, elementId) {
    /// <summary>
    /// Show or hide editor for a div
    /// </summary>
    /// <remarks>Do not modify, reference script is in DynaContentAdmin.js</remarks>
    /// <param name="show" type="boolean">True shows, False hides</param>
    /// <param name="elementId" optionnal>div id</param>

    //#region Removed for this business feature
    /*

    var content = "";
    var lastLayoutSlot;

    if (elementId == undefined || elementId == "") {
        if (lastLayoutSlotId != "") {
            elementId = lastLayoutSlotId;
        } else {
            var layoutSlots = $(".layoutSlot");
            if (layoutSlots.length > 0) {
                elementId = layoutSlots[0].id;
            } else {
                showIt = false;
            }
        }
    }

    $.fn.TinyMCEContentTransfertToSlot();
    */
    //#endregion

    if (showIt) {
        //#region Removed for this business feature
        /*
        var slotToEdit = $("#" + elementId);
        if (tinymce.editors.length == 0) {
            //TODO verifier mais ne doit plus arriver...
            tinyMCEElements = elementId;
            $.fn.TinyMCECreateTinyMCEInstance();
        }
        lastLayoutSlotId = elementId;
        lastLayoutSlot = $("#" + lastLayoutSlotId);
        if (lastLayoutSlot.length > 0) {
            if (tinymce.editors.length > 0) {
                tinymce.editors[0].setContent(lastLayoutSlot.html());
            }
        }

        var ed = tinyMCE.editors[0];
        var edContainer = $("#divFooEditor_parent");
        var edTable = $("#divFooEditor_tbl");
        var edIFrame = $("#" + ed.editorId + "_ifr");
        var slotWidth = slotToEdit.width();
        var slotHeight = slotToEdit.height();
        var mceStatusBarPosYFix = 0;
        var mceStatusBarPosXFix = 0;
        var edContainerOffset = slotToEdit.offset();
        var edContainerLeft = edContainerOffset.left;
        var edContainerTop = edContainerOffset.top;
        edContainer.css("position", "absolute");
        edContainer.css("z-index", slotToEdit.zIndex() + 1);
        edContainer.css("float", "left");
        edContainer.css("left", parseInt((edContainerLeft) + "px"));
        edContainer.css("top", parseInt((edContainerTop) + "px"));
        edContainer.css("width", (slotWidth + 4) + "px");

        edTable.removeClass("notDisplayed");

        edTable.css("width", (slotWidth + mceStatusBarPosXFix) + "px");

        edIFrame.css("width", (slotWidth + mceStatusBarPosXFix) + "px");

        edContainer.css("height", (slotHeight - mceStatusBarPosYFix + 14) + "px");
        edTable.css("height", (slotHeight + mceStatusBarPosYFix) + "px");
        edIFrame.css("height", (slotHeight + mceStatusBarPosYFix) + "px");
        edContainer.css("display", "block");
        $.fn.TinyMCEAddLayoutStyle();
        tinyMCELastElements = elementId;
        $("#divFooEditor_resize").css("display", "none");
        ed.focus();
        $(".mceEditor").css("background-color", slotToEdit.css("background-color"));
        //slotToEdit.css("display","none")
        */
        //#endregion
    } else {
        if (tinymce.editors.length > 0) {
            tinyMCE.DOM.hide();
            $("#divFooEditor_parent").css("display", "none");
        }
        tinyMCEElements = 'divFooEditor';
        //Verify if toolbar created
        var tr = $("#divMceRibbonBarPlaceholder").find(".mceLayout").find("tr");
        if (tr.length == 0) {
            $.fn.TinyMCECreateTinyMCEInstance();
        }
        //$(".layoutSlot").css("display", "block");
    }
}


//#endregion

//#region Ribbon stuff

$.fn.RibbonEditionTypeWritingToggleEditModeClick = function (evt) {
    /// <summary>
    /// Change Edit mode to structure mode
    /// </summary>
    /// <param name="evt"></param>
    var currentUseMode = UseMode.getCurrent();
    if (currentUseMode != enUseModes.structureEditMode) {
        $.fn.TinyMCEShow(false);
        $(".AdminModeMenuButtonRight,.AdminModeMenuButtonLeft").css("display", "");
        $(".innoToggleEdit_Edit").addClass("innoToggleEdit_Preview");
        $(".innoToggleEdit_Edit").removeClass("innoToggleEdit_Edit");
        $(".LastMenuBarItem").parent().show();
        UseMode.setCurrent(enUseModes.structureEditMode);
    } else {
        $(".AdminModeMenuButtonRight,.AdminModeMenuButtonLeft").css("display", "none");
        $(".innoToggleEdit_Preview").addClass("innoToggleEdit_Edit");
        $(".innoToggleEdit_Preview").removeClass("innoToggleEdit_Preview");
        $(".LastMenuBarItem").parent().hide();
        $("#divBody").addClass("editMode");
        UseMode.setCurrent(enUseModes.contentEditMode);
    }
    if (evt != null) {
        $.fn.EnsureStopPropagation(evt);
    }
}

$.fn.RibbonEditionLayoutGetAndInsertLayoutClick = function (evt) {
    /// <summary>
    /// Load pre build layout
    /// </summary>
    /// <param name="evt"></param>
    var buttonId = $(arguments[0].srcElement || arguments[0].target).parent().attr("id");
    var pos = buttonId.indexOf("_innoInsert");
    layoutCommand = buttonId.substring(pos + 11);
    if ($.fn.EditionIsThereSomeContent()) {
        $.fn.jqDialogConfirm(cap_ConfirmLayoutSlotNotEmptyContentLostIf_Title, cap_ConfirmLayoutSlotNotEmptyContentLostIf_Message, cap_YesButtonCaption, cap_NoButtonCaption, $.fn.ContentLayoutGetPreBuildContentLayout, 450, 150);
    } else {
        $.fn.ContentLayoutGetPreBuildContentLayout(evt);
    }
}

$.fn.RibbonEditionLayoutRemoveLayoutClick = function (evt) {
    /// <summary>
    /// Obsolete
    /// </summary>
    /// <param name="evt"></param>
    var edTmp = $.fn.jqDialogPrepareAndGet('edTmp', null);
    $("#edTmp").html($.fn.TinyMCEGetContentHack());
    $("#edTmp").find(".MyPrettyCMSContentLayout").each(function () {
        $(this).replaceWith(
        function () {
            return "<div>" + $(this).html() + "</div>";
        })
    });
    tinymce.activeEditor.setContent(edTmp.html());
}

$.fn.RibbonLayoutAddGridsterWidgetClick = function () {
    /// <summary>
    /// Add a new Content layout slot
    /// </summary>
    var htmlBribe = '<li data-row="1" data-col="1" data-sizex="2" data-sizey="2" class="mceNonEditable gridsterLi MyPrettyCMSSlot">' + gridsterLayoutSlotDropHtml + '<div class="mceEditable"></div></li>';
    gridster.add_widget(htmlBribe);
}

$.fn.RibbonEditionTypeWritingSaveContentClick = function (evt) {
    /// <summary>
    /// Save content ribbon button click handle
    /// </summary>
    /// <param name="evt"></param>
    var currentUseMode = UseMode.getCurrent();
    switch (currentUseMode) {
        case enUseModes.structureEditMode:
            break;
        case enUseModes.contentEditMode:
            $.fn.jqDialogFunction("divStructurePage", StructurePageEditorTitle, $.fn.EditionTypeWritingSaveContent, null, null, 800, 450, null, true);
            break;
        case enUseModes.contentCodeMode:
            //Check if contentCode modified
            break;
        case enUseModes.contentLayoutMode:
            //Check if contentLayout modified
            break;
        case enUseModes.settingsMode:
            //Check if settings modified
            break;
        case enUseModes.siteLayoutMode:
            //Check if site layout modified
            break;
    }
}

$.fn.RibbonSettingsGeneralClick = function (evt) {
    /// <summary>
    /// Ribbon button click handler : Settings tab General Settings
    /// </summary>
    /// <param name="evt"></param>
    $.fn.jqDialogFunction("AdminSettingsDialogDiv", cap_Settings_AdminSettingsDialog_EditFormTitle, $.fn.AdminSettingsDialogOkClick, null, $.fn.AdminSettingsDialogFeed, 650, 620, null, false, true);
}

$.fn.RibbonSettingsPlugInsClick = function (evt) {
    /// <summary>
    /// Ribbon button click handler : Plug ins
    /// </summary>
    /// <param name="evt"></param>
    $.fn.jqDialogAlert("Plug ins", "Coming soon !");
}

$.fn.RibbonSettingsSiteLayoutClick = function (evt) {
    /// <summary>
    /// Ribbon button click handler : Site layout
    /// </summary>
    /// <param name="evt"></param>
    if (UseMode.getCurrent() != enUseModes.siteLayoutMode) {
        UseMode.setCurrent(enUseModes.siteLayoutMode);
    } else {
        UseMode.setCurrent(enUseModes.settingsMode);
    }
}

$.fn.RibbonSettingsAdminRolesClick = function (evt) {
    /// <summary>
    /// Ribbon button click handler : AdminRoles
    /// </summary>
    /// <param name="evt"></param>
    evt.preventDefault();
    $.fn.SettingsAdminRolesDialog();
}

$.fn.RibbonSettingsUsersClick = function (evt) {
    /// <summary>
    /// Ribbon button click handler : AdminUsers
    /// </summary>
    /// <param name="evt"></param>
    evt.preventDefault();
    $.fn.SettingsUsersDialog();
    $.fn.EnsureStopPropagation(evt);
    return false;
}

$.fn.RibbonSettingsFileManagmentClick = function (evt) {
    /// <summary>
    /// Open the files manager
    /// </summary>
    /// <param name="evt"></param>
    var fileBrowserURL = "/" + uiLanguage + "/finder";
    $.fn.jqDialogContainer("divFinder", "File management", $.fn.prepareFileManagementIFrame, null, 980, 690, "<iframe src='" + fileBrowserURL + "' frameborder='0' class='fileManagementIFrame' scrolling='no'></iframe>");
}

$.fn.RibbonTabClick = function (evt, ui) {
    /// <summary>
    /// Ribbon events handler
    /// </summary>
    /// <param name="evt"></param>
    /// <param name="ui"></param>
    currentTabIndex = $(this).index();
    var currentUseMode = UseMode.getCurrent();
    switch (currentTabIndex) {
        case enRibbonTabs._00Start:
            switch (currentUseMode) {
                case enUseModes.structureEditMode:
                    //The UseMode is already structureEditMode
                    break;
                default:
                    UseMode.setCurrent(enUseModes.contentEditMode);
                    break;
            }
            break;
        case enRibbonTabs._01Insert:
        case enRibbonTabs._04Image:
        case enRibbonTabs._05Table:
            UseMode.setCurrent(enUseModes.contentEditMode);
            break;
        case enRibbonTabs._02Layout:
            UseMode.setCurrent(enUseModes.contentLayoutMode);
            break;
        case enRibbonTabs._03Code:
            UseMode.setCurrent(enUseModes.contentCodeMode);
            break;
        case enRibbonTabs._06Settings:
            UseMode.setCurrent(enUseModes.settingsMode);
            break;
    }
}


//#endregion

